/*
 * Copyright (C) 2022 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef DESKTOPITEMLOADER_H
#define DESKTOPITEMLOADER_H

#include <QObject>
#include <ukui/kappwidgetmanager.h>
#include "types.h"
#include "config.h"
#include "utils.h"

class DesktopFileManager;
class DesktopItemLoader : public QObject
{
    Q_OBJECT
public:
    struct DesktopLayout {
        int row;
        int column;
    };

    explicit DesktopItemLoader(DesktopFileManager *desktopFileManager,
                               DesktopLayout desktopLayout,
                               QObject *parent = nullptr);

    void load();

    UKUI::ItemMap allItems() const
    { return m_allItems; }

    UKUI::ItemIdInSetMap itemIdsInSet() const
    { return m_itemIdsInSet; }

    UKUI::ItemIdInHomeScreenMap allItemIdsInHomeScreen() const
    { return m_allItemIdsInHomeScreen; }

    UKUI::ItemInPageGridMap itemIdsInPageGrid() const
    { return m_itemIdsInPageGrid; }

    QList<UKUI::ItemId> appWidgetIds() const
    { return m_appWidgetIds; }

protected:
    struct ItemPositionInfo
    {
        int itemId;
        int page;
        int startRow;
        int startColumn;
        int rowSpan;
        int columnSpan;
    };
    void placeItem(const ItemPositionInfo &itemPosition);

    virtual void loadLocalItems() = 0;
    virtual void placeItems() = 0;

private:
    void dissectDesktopFilesToItems();

protected:
    UKUI::ItemMap m_allItems;
    UKUI::ItemIdInSetMap m_itemIdsInSet;
    UKUI::ItemIdInHomeScreenMap m_allItemIdsInHomeScreen;
    UKUI::ItemInPageGridMap m_itemIdsInPageGrid;
    UKUI::CategoryStringMap m_softwareStoreAppCategory;
    QList<UKUI::ItemId> m_appWidgetIds;
    DesktopFileManager *m_desktopFileManager;
    Config m_config;
    DesktopLayout m_desktopLayout;
};

class TaskbarAppManager;
class TabletDesktopItemLoader : public DesktopItemLoader
{
    Q_OBJECT

public:
    explicit TabletDesktopItemLoader(DesktopFileManager *desktopFileManager,
                                     TaskbarAppManager *taskbarManger,
                                     DesktopLayout desktopLayout,
                                     AppWidget::KAppWidgetManager *appWidgetManager,
                                     QObject *parent = nullptr);

protected:
    void loadLocalItems() override;
    void placeItems() override;

private:
    void loadTaskbarApps();
    void loadDefaultGroup();
    void loadAppWidget();
    void loadGroupFromConfig(const QString &groupName, const QStringList &desktopFiles);

    void placeAppWidget(UKUI::ItemId appWidgetId);
    void placeGroup(UKUI::ItemId groupId);
    void placeAppIcons();

private:
    TaskbarAppManager *m_taskbarAppManager;
    AppWidget::KAppWidgetManager *m_appWidgetManager;
    const int kTaskbarGroupId = 0;
};

class EduScreenDesktopItemLoader : public DesktopItemLoader
{
    Q_OBJECT

public:
    explicit EduScreenDesktopItemLoader(DesktopFileManager *desktopFileManager,
                                        DesktopLayout desktopLayout,
                                        QObject *parent = nullptr);

protected:
    void loadLocalItems() override;
    void placeItems() override;

private:
    void loadDefaultContainer();
    void loadContainerFromConfig(const Config::ContainerInfo containerInfo);
    void placeContainer(UKUI::ItemId containerId);
};

#endif // DESKTOPITEMLOADER_H
